package hotel.compare.oversea.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import hotel.base.oversea.dto.meituan.MtRoomInfoDto;
import hotel.base.oversea.utils.JacksonUtil;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.vo.meituan.request.room.MtHotelRealRoomVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 相关restTemplate 接口调用工具实现类
 *
 * @Author lmf
 * @Date 2020/8/5 10:29
 **/
@Slf4j
@Service
public class CompareRestTempaleUtilsService {
    @Autowired
    private RestTemplate restTemplate;
    /***
     * @Author: lmf
     * @Date: 2020/12/15
     * 获取美团的房型列表
     */
    public Result<List<MtRoomInfoDto>> sendPostToMeiTuanHotelRoomList(String url, Map<String, Object> params) {
        log.info("=====请求美团的房型列表接口开始=====");
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        log.info("===========请求的url为{}============",url);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> request = new HttpEntity<>(params, headers);
        log.info("==========请求接口参数为{}", JSON.toJSONString(params));
        ParameterizedTypeReference<Result<Object>> typeRef = new ParameterizedTypeReference<Result<Object>>() {
        };
        ResponseEntity<Result<Object>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        log.info("==============返回的数据为{}",responseEntity);
        JSONObject jsonObject= (JSONObject) JSON.parse(JSON.toJSONString(responseEntity.getBody()));
        List<MtRoomInfoDto> mtRoomInfoDtos= JacksonUtil.jsonToList(jsonObject.getString("data"),MtRoomInfoDto.class);
        return new Result<>(mtRoomInfoDtos);
    }

    public Result<List<MtHotelRealRoomVo>> sendPostToMeiTuanHotelRoomReturnList(String url, Map<String, Object> params) {
        log.info("=====请求美团的房型列表接口开始=====");
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        log.info("===========请求的url为{}============",url);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> request = new HttpEntity<>(params, headers);
        log.info("==========请求接口参数为{}", JSON.toJSONString(params));
        ParameterizedTypeReference<Result<Object>> typeRef = new ParameterizedTypeReference<Result<Object>>() {
        };
        ResponseEntity<Result<Object>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        log.info("==============返回的数据为{}",responseEntity);
        JSONObject jsonObject= (JSONObject) JSON.parse(JSON.toJSONString(responseEntity.getBody()));
        List<MtHotelRealRoomVo> mtRoomInfoDtos=JacksonUtil.jsonToList(jsonObject.getString("data"),MtHotelRealRoomVo.class);
        return new Result<>(mtRoomInfoDtos);
    }
}
